﻿using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace NfsBLL
{
  public  class ShareBLL
    {        private readonly NfsDAL.ShareDAL dal = new NfsDAL.ShareDAL();

    public ShareBLL()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			return dal.Exists(id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(NfsModel.share model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(NfsModel.share model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(Maticsoft.Common.PageValidate.SafeLongFilter(idlist,0) );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public NfsModel.share GetModel(int id)
		{
			
			return dal.GetModel(id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
        public NfsModel.share GetModelByCache(int id)
		{
			
			string CacheKey = "shareModel-" + id;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
            return (NfsModel.share)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
        public List<NfsModel.share> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
        public List<NfsModel.share> DataTableToList(DataTable dt)
		{
            List<NfsModel.share> modelList = new List<NfsModel.share>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
                NfsModel.share model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
        public NfsModel.share GetModel_nonceStr(string nonceStr)
        {
            return dal.GetModel_nonceStr(nonceStr);
        }
        #region PC模糊查询分页
        public List<share> GetShareBycondtion(int pageindex, int pagesize, string Name, int state,int parent_id)
        {
            DataTable dt = dal.GetShareBycondtion(pageindex, pagesize, Name, state,parent_id);
            return ConverToExcs.ConvertToEx<share>(dt);
        }
        #endregion
        #region  修改是否审核
        public bool update_Shenhe(share model,int agency_id)
        {

            bool flag = true;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    if (model != null)
                    {
                        flag = this.Update(model);
                        if (flag)
                        {
                            AgencyBLL ag_bll=new AgencyBLL();
                            agency ag_model=new agency{ id=agency_id,shState=1};
                            flag = ag_bll.Update_shState(ag_model);
                        }
                        if (flag)
                        {
                            trans.Complete();
                        }
                    }
                    else
                    {
                        flag = false;
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
         #endregion
      #region 根据经销id修改信息审核状态
      
        public bool Update_state(int state,int agency_id)
        {
            return dal.Update_state(state,agency_id);
        }

        #endregion 
		#endregion  ExtensionMethod
	}
}

